/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.order.service.impl;

import me.zhengjie.modules.order.domain.WechatOrder;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.order.repository.WechatOrderRepository;
import me.zhengjie.modules.order.service.WechatOrderService;
import me.zhengjie.modules.order.service.dto.WechatOrderDto;
import me.zhengjie.modules.order.service.dto.WechatOrderQueryCriteria;
import me.zhengjie.modules.order.service.mapstruct.WechatOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
*
* @description 服务实现
* @author maojinhui
* @date 2020-09-08
**/
@Service
@RequiredArgsConstructor
public class WechatOrderServiceImpl implements WechatOrderService {

    private final WechatOrderRepository wechatOrderRepository;
    private final WechatOrderMapper wechatOrderMapper;

    @Override
    public Map<String,Object> queryAll(WechatOrderQueryCriteria criteria, Pageable pageable){
        Page<WechatOrder> page = wechatOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(wechatOrderMapper::toDto));
    }

    @Override
    public List<WechatOrderDto> queryAll(WechatOrderQueryCriteria criteria){
        return wechatOrderMapper.toDto(wechatOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public WechatOrderDto findById(String id) {
        WechatOrder wechatOrder = wechatOrderRepository.findById(id).orElseGet(WechatOrder::new);
        ValidationUtil.isNull(wechatOrder.getId(),"WechatOrder","id",id);
        return wechatOrderMapper.toDto(wechatOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WechatOrderDto create(WechatOrder resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return wechatOrderMapper.toDto(wechatOrderRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WechatOrder resources) {
        WechatOrder wechatOrder = wechatOrderRepository.findById(resources.getId()).orElseGet(WechatOrder::new);
        ValidationUtil.isNull( wechatOrder.getId(),"WechatOrder","id",resources.getId());
        wechatOrder.copy(resources);
        wechatOrderRepository.save(wechatOrder);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            wechatOrderRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<WechatOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WechatOrderDto wechatOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", wechatOrder.getUserId());
            map.put("发布类型", wechatOrder.getAttach());
            map.put("发布类型对应的id", wechatOrder.getAttachId());
            map.put("发布时间对应的id", wechatOrder.getPublishMoneyId());
            map.put("订单信息", wechatOrder.getBody());
            map.put("总支付金额（单位：分）", wechatOrder.getTotalFee());
            map.put("交易类型", wechatOrder.getTradeType());
            map.put("发起支付ip", wechatOrder.getSpbillCreateIp());
            map.put("订单状态0:待支付；-1.支付失败，1支付成功", wechatOrder.getStatus());
            map.put("微信支付订单号", wechatOrder.getTransactionId());
            map.put("支付完成时间", wechatOrder.getEndTime());
            map.put("创建时间", wechatOrder.getCreateTime());
            map.put("更新时间", wechatOrder.getUpdateTime());
            map.put("创建人", wechatOrder.getCreateBy());
            map.put("更新人", wechatOrder.getUpdatedBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}